﻿
import { Dialog } from 'primeng/components/dialog/dialog';
import { Component, Injector, ViewChild, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RoleListDto, RoleServiceProxy, ContactDto, ContactServiceProxy, TemplateServiceProxy, WorkStationDto, WorkStationUpdateOrOutput, WorkingTeamServiceProxy, WorkingTeamDto, CreateWorkingTeam, GetLineInstanceForDistributeOutput, WorkingTeamProportion, AddInstancesToWorkingTeamInput } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
    selector : 'app-contact',
    templateUrl: './distribute.component.html',
    styleUrls: ['./distribute.component.less'],
    animations: [appModuleAnimation()]
})
export class DistributeComponent extends AppComponentBase implements OnInit  {

    instances: GetLineInstanceForDistributeOutput[] = [];
    workingTeams:   WorkingTeamProportion [] = [];
    selectedAll = false;

    constructor(
        injector: Injector,
        private _workingTeamServiceProxy: WorkingTeamServiceProxy
        ) 
        {
            super(injector);            
        }
    
    
    ngOnInit(): void {
            
       this.getAllInstanceOfDistribute();
        this.getAllWorkingTeam();
    }

      
    getAllInstanceOfDistribute(){

        this._workingTeamServiceProxy.getAllInstanceOfDistribute(true).subscribe((result)=>{
            
            this.instances = result;
        });
    }

    getAllWorkingTeam(){
        this._workingTeamServiceProxy.getProportions().subscribe((result)=>{            
            this.workingTeams = result;
        });
    }


    changeSelectAll() {
        this.selectedAll = !this.selectedAll;

        this.instances.forEach(element => {
            element.isSelected = this.selectedAll;
        });
    }

    substributeTo(teamId :number)
    {
        let intancesForSubstribute:AddInstancesToWorkingTeamInput[] = [];
        this.instances.forEach(element => {
            if(element.isSelected)
            {
                let iATeam = new AddInstancesToWorkingTeamInput();
                
                
                iATeam.instanceId = element.id;
                iATeam.workTeamaId = teamId;
                intancesForSubstribute.push(iATeam);
            }
        });

        this._workingTeamServiceProxy.addInstancesToWorkingTeam(intancesForSubstribute).subscribe(()=>{
            this.notify.info('分配成功！');
            this.getAllInstanceOfDistribute();
            this.getAllWorkingTeam();
        })

    }
}

